package org.example.repeater.web.model;
import java.io.Serializable;

/**
 * 操作结果实体
 * @author sky
 * @date 2022/9/19
 */

public class Response<T> implements Serializable {
    private static final long serialVersionUID = -4353042225436721264L;
    /**
     * 返回码
     */

    private Integer code;

    /**
     * 操作结果信息
     */

    private String msg;

    /**
     * 返回实体 T
     */

    private T data;



    /**
     * 操作成功方法（返回提示信息）
     *
     * @param msg 提示信息
     * @return 操作成功 返回提示信息
     */
    public static Response ok(String msg) {
        // 返回提示信息
        return Response.builder().code(ResponseStatusEnum.SUCCESS.getCode()).msg(msg).build();
    }


    /**
     * 操作成功方法（返回成功结果实体列表）
     *
     * @param data 操作返回实体列表
     * @param msg  操作提示信息
     * @return 操作成功结果实体
     */
    public static <T> Response<T> ok(T data, String msg) {
        // 返回操作成功结果实体
        return Response.<T>builder().code(ResponseStatusEnum.SUCCESS.getCode()).data(data).msg(msg).build();
    }

    /**
     * 操作失败方法
     *
     * @param msg 操作提示信息
     * @return 操作失败结果实体
     */
    public static Response error(String msg) {
        // 返回操作失败结果实体
        return Response.builder().code(ResponseStatusEnum.ERROR.getCode()).msg(msg).build();
    }

    /**
     * 操作失败方法（返回失败）
     *
     * @param data 操作返回实体列表
     * @param msg  操作提示信息
     * @return 操作失败结果实体
     */
    public static <T> Response<T> error(T data, String msg) {
        // 返回操作失败结果实体
        return Response.<T>builder().code(ResponseStatusEnum.ERROR.getCode()).data(data).msg(msg).build();
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
    Response(final Integer code, final String msg, final T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ResponseBuilder<T> builder() {
        return new ResponseBuilder();
    }

    public static class ResponseBuilder<T> {
        private Integer code;
        private String msg;
        private T data;

        ResponseBuilder() {
        }

        public ResponseBuilder<T> code(final Integer code) {
            this.code = code;
            return this;
        }

        public ResponseBuilder<T> msg(final String msg) {
            this.msg = msg;
            return this;
        }

        public ResponseBuilder<T> data(final T data) {
            this.data = data;
            return this;
        }

        public Response<T> build() {
            return new Response(this.code, this.msg, this.data);
        }

        public String toString() {
            return "Response.ResponseBuilder(code=" + this.code + ", msg=" + this.msg + ", data=" + this.data + ")";
        }
    }




}